/* Include files */
#include "math.h"
#include "mex.h"

/* Include to sort : http://stackoverflow.com/questions/1787996/c-library-function-to-do-sort */
#include <stdio.h>
#include <stdlib.h>


/* Checks whether inputs are matrices */
#define IS_REAL_2D_FULL_DOUBLE(P) (!mxIsComplex(P) && \
mxGetNumberOfDimensions(P) == 2 && !mxIsSparse(P) && mxIsDouble(P))
#define IS_REAL_SCALAR(P) (IS_REAL_2D_FULL_DOUBLE(P) && mxGetNumberOfElements(P) == 1)

/*************************************************/
/* MAIN */
/*************************************************/

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
	/* Define output matrix */
	#define rowOUT plhs[0]

	/* Define input matrices */
	#define rowIN  prhs[0]

	int a, b;

	/* Check the number of inputs is correct (Aoffer,Baccept,QbarA) */
	if(nrhs < 1)
		mexErrMsgTxt("Too few input arguments.");
	else if(nrhs > 1)
		mexErrMsgTxt("Too many input arguments.");

	if(!IS_REAL_2D_FULL_DOUBLE(rowIN))
		mexErrMsgTxt("Aoffer must be real 2D full double array.");
	
	/* Dimensions and pointers for inputs */
	int A = mxGetM(rowIN);
	double *in_pt = mxGetPr(rowIN);

	/* Assignment test */
/*	printMatEl(A_pt,Bset_pt,A,B); */

/***************************************************************/
/* Begin Sorting */
/***************************************************************/

	b = 0;
	for (a = 0; a < A; a++)
	{
		if (in_pt[a] != 0) 
			b++;
	}

	/* Output matrix */
	rowOUT = mxCreateDoubleMatrix(b,1,mxREAL);
	double *out_pt = mxGetPr(rowOUT);

	b = 0;
	for (a = 0; a < A; a++)
	{
		if (in_pt[a] != 0) 
		{
			out_pt[b] = in_pt[a]; 
			b++;
		}
	}

	/* Check input */
/*  mexCallMATLAB(0,NULL,1, &Aoffer, "disp"); */
/*	return 0; */

}


